package cn.lanqiao.web.controller.business;

import cn.lanqiao.business.domain.ClassManagement;
import cn.lanqiao.business.domain.Semester;
import cn.lanqiao.business.domain.Student;
import cn.lanqiao.business.domain.vo.StudentVo;
import cn.lanqiao.business.domain.vo.StudnetPointsVo;
import cn.lanqiao.business.service.IStudentService;
import cn.lanqiao.common.annotation.Log;
import cn.lanqiao.common.core.controller.BaseController;
import cn.lanqiao.common.core.domain.AjaxResult;
import cn.lanqiao.common.core.domain.entity.SysUser;
import cn.lanqiao.common.core.page.TableDataInfo;
import cn.lanqiao.common.enums.BusinessType;
import cn.lanqiao.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 学生信息Controller
 * 
 * @author yangjianqing
 * @date 2024-07-08
 */
@RestController
@RequestMapping("/business/student")
public class StudentController extends BaseController
{
    @Autowired
    private IStudentService studentService;

    /**
     * 查询学生信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:student:list')")
    @GetMapping("/list")
    public TableDataInfo list(Student student)
    { 
        startPage();
        List<Student> list = studentService.selectStudentListToExport(student);
        return getDataTable(list);
    }

    /**
     * 导出学生信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:student:export')")
    @Log(title = "学生信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Student student)
    {
        List<Student> list = studentService.selectStudentListToExport(student);
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        util.exportExcel(response, list, "学生信息数据");
    }

    /**
     * 获取学生信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:student:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(studentService.selectStudentById(id));
    }

    /**
     * 新增学生信息
     */
    @PreAuthorize("@ss.hasPermi('business:student:add')")
    @Log(title = "学生信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Student student)
    {
        return toAjax(studentService.insertStudent(student));
    }

    /**
     * 修改学生信息
     */
    @PreAuthorize("@ss.hasPermi('business:student:edit')")
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Student student)
    {
        return toAjax(studentService.updateStudent(student));
    }

    /**
     * 删除学生信息
     */
    @PreAuthorize("@ss.hasPermi('business:student:remove')")
    @Log(title = "学生信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(studentService.deleteStudentByIds(ids));
    }

    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('business:Studnet:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport,Long stuClassId) throws Exception
    {
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        List<Student> stus = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = studentService.importStudent(stus,stuClassId,operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<StudentVo> util = new ExcelUtil<StudentVo>(StudentVo.class);
        util.importTemplateExcel(response, "用户数据");
    }

    /**
     * 查询学生、学期、积分总分
     */
    @PreAuthorize("@ss.hasPermi('business:student:list')")
    @GetMapping("/pointslist")
    public TableDataInfo pointslist(@RequestParam(name = "stuName",required = false) String stuName,@RequestParam(name = "stuId",required = false) String stuId,@RequestParam(name = "claId",required = false) Long claId,@RequestParam(name = "semId",required = false) Long semId) {
        startPage();
        // 封装查询对象
        StudnetPointsVo studnetPointsVo = new StudnetPointsVo();
        studnetPointsVo.setStuName(stuName);
        studnetPointsVo.setStuId(stuId);
        ClassManagement cm = new ClassManagement();
        cm.setId(claId);
        studnetPointsVo.setStuClassManagement(cm);
        studnetPointsVo.setStuSemId(semId);
        // 查询
        List<StudnetPointsVo> list = studentService.selectStudentPointslist(studnetPointsVo);
        return getDataTable(list);
    }
}
